cd ..
source_code_dir=$(pwd)

declare -A needed       # 声明关联数组
needed=(
    ["build"]="build"
    ["e2fsprogs_source"]="third_party/e2fsprogs"
    ["packages"]="out/rk3568/packages"
    ["gen_build_ohos"]="out/rk3568/gen/build/ohos"
    ["e2fsprogs_output"]="out/rk3568/clang_x64/thirdparty/e2fsprogs"
    ["f2fs"]="out/rk3568/clang_x64/thirdparty/f2fs-tools"
    ["pcre2"]="out/rk3568/clang_x64/thirdparty/pcre2"
    ["selinux"]="out/rk3568/clang_x64/thirdparty/selinux"
    ["selinux_adapter"]="out/rk3568/obj/base/security/selinux_adapter"
    ["config_json"]="out/ohos_config.json"
    ["components_json"]="out/rk3568/build_configs/parts_info/components.json"
)

ABSOLUTE_SCRIPT_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")
script_dir=$(dirname "$ABSOLUTE_SCRIPT_PATH")
cd $script_dir
rm -rf for_package
mkdir for_package
cd for_package
pack_dir=$(pwd)

# 添加排除规则配置
# exclude_patterns=(
#     "phone/images/chip_ckm.img"
#     "phone/images/chip_prod.img"
#     "phone/images/eng_system.img"
#     "phone/images/ramdisk.img"
#     "phone/images/sys_prod.img"
#     "phone/images/system.img"
#     "phone/images/updater.img"
#     "phone/images/userdata.img"
#     "phone/images/vendor.img"
#     "phone/NOTICE_FILES"    # 排除phone/NOTICE_FILE文件
#     "phone/system.zip"           # 排除phone/system.zip文件
#     "phone/system_notice_files.zip"  # 排除phone/system_notice_files.zip文件
# )

# 检查rsync是否安装
if ! command -v rsync &> /dev/null; then
    echo "错误: 未安装rsync，请先安装rsync工具"
    exit 0
fi

for key in "${!needed[@]}"; do
    need_path=${needed[$key]}
    source_path=$source_code_dir/$need_path
    # source_path="${source_path/out/raw_out}"
    dest_path=$pack_dir/$need_path
    parent_dir=$(dirname $dest_path)
    echo "mkdir -p $parent_dir"
    mkdir -p $parent_dir
    echo "Copying $source_path to $parent_dir"
    # if [ $key == "packages" ]; then
    #     # 特殊处理build目录，使用rsync排除特定目录和文件
    #     exclude_args=()
    #     for pattern in "${exclude_patterns[@]}"; do
    #         exclude_args+=(--exclude="$pattern")
    #     done
    #     rsync -a "${exclude_args[@]}" "$source_path/" "$parent_dir/packages"
    # else
    #     # 其他目录直接使用cp命令
    #     cp -rf $source_path $parent_dir
    # fi
    cp -rf $source_path $parent_dir
done
cd ../
rm -rf for_package.tar.gz
tar -czf for_package.tar.gz for_package
echo "rm -rf $source_code_dir/out/part_so/for_package.tar.gz"
rm -rf $source_code_dir/out/part_so/for_package.tar.gz
echo "Packing completed, moving for_package.tar.gz to $source_code_dir/out/part_so"
mkdir -p $source_code_dir/out/part_so
mv for_package.tar.gz $source_code_dir/out/part_so
echo "Move completed."
